﻿using System;
using System.Collections.Generic;
using System.Text;

namespace POSPlus.EZBarTender.DatabaseOperations
{
	/// <summary>
	/// Base for object based table connections.
	/// </summary>
	public abstract class DatabaseObject
	{
		public String ConnectionString
		{
			get;
			protected set;
		}

		/// <summary>
		/// Populates the database object with the default values from the database.
		/// </summary>
		protected abstract void PopulateDefault();

		/// <summary>
		/// Inserts the database object with the objects values into the database table.
		/// </summary>
		public abstract Boolean Create();

		/// <summary>
		/// Updates the database with the values in this object for the corresponding record.
		/// </summary>
		public abstract Boolean Update();

		/// <summary>
		/// Removes the database object from the database.
		/// </summary>
		public abstract Boolean Delete();

		/// <summary>
		/// Returns the database object casted to the given type.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="value"></param>
		/// <returns></returns>
		public static T FromDB<T>(object value)
		{
			return Convert.IsDBNull(value) ? default(T) : (T)value;
		}

		public static object ToDB<T>(T value)
		{
			return value == null ? (object)DBNull.Value : value;
		}
	}
}
